package entity

import (
	"context"
	"time"

	"github.com/LyricTian/gin-admin/v7/internal/app/schema"
	"github.com/LyricTian/gin-admin/v7/pkg/util/structure"
	"github.com/jinzhu/gorm"
)

// GetGroupDB 获取group存储
func GetGroupDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
	return GetDBWithModel(ctx, defDB, new(Group))
}

// SchemaGroup group对象
type SchemaGroup schema.Group

// ToGroup 转换为group实体
func (a SchemaGroup) ToGroup() *Group {
	item := new(Group)
	structure.Copy(a, item)
	return item
}

// Group group实体
type Group struct {
	ID        string     `gorm:"column:id;primary_key;size:36;"`
	Name      string     `gorm:"size:120;column:name;"`         // 群名称
	HeadImg   string     `gorm:"size:255;column:head_img;"`     // 头像
	Type      int        `gorm:"column:type;size:11;"`          // 群类型(1:21天群 2:高级群 3:朗读群)
	Tag       string     `gorm:"column:tag;size:255;"`          // 分组标签
	Memo      string     `gorm:"size:512;column:memo;"`         // 群简介
	Status    int        `gorm:"column:status;size:11;"`        // 状态(1:启用 2:停用)
	Admin     string     `gorm:"column:admin;size:36;index;"`   // 群管理
	Creator   string     `gorm:"size:36;index;column:creator;"` // 创建者
	CreatedAt time.Time  `gorm:"column:created_at;index;"`
	UpdatedAt time.Time  `gorm:"column:updated_at;index;"`
	DeletedAt *time.Time `gorm:"column:deleted_at;index;"`
}

// ToSchemaGroup 转换为group对象
func (a Group) ToSchemaGroup() *schema.Group {
	item := new(schema.Group)
	structure.Copy(a, item)
	return item
}

// Groups group列表
type Groups []*Group

// ToSchemaGroups 转换为group对象列表
func (a Groups) ToSchemaGroups() []*schema.Group {
	list := make([]*schema.Group, len(a))
	for i, item := range a {
		list[i] = item.ToSchemaGroup()
	}
	return list
}
